#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include "x509-util.h"

int tX509_fun()
{
    char * certSN;
	char * start_time;
	char * end_time;
	char * subject_cn;

	char * bindinfo;

    char * subnameString = NULL;

    X509_CERT_SUBJECT_T cert_subject;

    X509 * userCert;
    userCert = x509_init_cert("./certs/test.cer");

    if(NULL == userCert)
    {
        printf("[x509t test] userCert null\n");
        goto finish;
    }

    subject_cn =x509_cert_subject_CN(userCert);

    printf("[x509 test] x509_cert_subject_CN subject CN name is ##%s\n", subject_cn);


    x509_cert_subject_ele(userCert, &cert_subject);

    printf("[x509 test] x509_cert_subject_string cert subject CN name ##%s\n", cert_subject.common_name);

    bindinfo = x509_cert_bindinfo(userCert, &cert_subject);
    printf("[x509 test] x509_cert_bindinfo is ##%s\n", bindinfo);


    start_time = x509_cert_start_time(userCert);
    end_time = x509_cert_end_time(userCert);

    printf("[x509 test] cert start time ##%s\n", start_time);
    printf("[x509 test] cert end time ##%s\n", end_time);

    certSN = x509_cert_sn(userCert);

    if(certSN == NULL)
    {
        printf("[x509 test] get cert sn failed\n");
        goto finish;
    }
    printf("[x509 test] cert sn is ####%s\n", certSN);



finish:

    if(subject_cn)
        free(subject_cn);

    if(subnameString)
        free(subnameString);

    if(start_time)
        free(start_time);

    if(end_time)
        free(end_time);

    if(certSN)
        free(certSN);


    if(userCert != NULL)
        x509_free_cert(userCert);

    return 0;

}
int main(int argc, char ** argv)
{
    char * str = NULL;
    char str_buf[128] = {0};

    str = strcat(str_buf, "a");
    str = strcat(str_buf, "b");

    str = strcat(str_buf, "12345");
    printf("{main] #####%s\n", str);
    printf("{main] #####%s\n", (const char *)str_buf);


    return tX509_fun();

}
